package edu.zut.hys.picturescanner.slice;

import edu.zut.hys.picturescanner.ResourceTable;
import edu.zut.hys.picturescanner.bean.ImageLineItem;
import edu.zut.hys.picturescanner.provider.ImageLineProvider;
import edu.zut.hys.picturescanner.utils.LogUtil;
import edu.zut.hys.picturescanner.utils.PictureManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.bundle.IBundleManager;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.hiviewdfx.HiLog;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PropertyKey;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;
import ohos.security.SystemPermission;
import ohos.utils.net.Uri;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private static final String TAG = MainAbilitySlice.class.getSimpleName();

    private List<Uri> imagePathElements = null;

    private PictureManager pictureManager;

    private ImageLineProvider imageLineProvider;

    private ListContainer listContainer;

    private List<ImageLineItem> imageLineItemList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        requestPermission();

        initData();
        super.setUIContent(ResourceTable.Layout_ability_main);
        initComponents();
        initListContainer();
    }

    private void initComponents(){

        listContainer= (ListContainer) findComponentById(ResourceTable.Id_list_container);

    }

    private void initListContainer() {
        listContainer.setReboundEffect(true);
        imagePathElements = pictureManager.getimageElements();

        LogUtil.info(TAG,"imagePathElements.size() : "+imagePathElements.size());

        imageLineItemList = new ArrayList<>();
        for(int i = 0; i < 20; i++){
            ImageLineItem imageLineItem = new ImageLineItem(i);
            Uri[] uris = new Uri[3];
            uris[0] = imagePathElements.get(i*3);
            uris[1] = imagePathElements.get(i*3+1);
            uris[2] = imagePathElements.get(i*3+2);
            imageLineItem.setUris(uris);
            imageLineItemList.add(imageLineItem);
        }
        LogUtil.info(TAG,"imageLineItemList.size() : "+imageLineItemList.size());
        imageLineProvider = new ImageLineProvider(imageLineItemList,this);

        listContainer.setItemProvider(imageLineProvider);
    }

    private void initData(){
        pictureManager = new PictureManager(getApplicationContext());
        imagePathElements = pictureManager.getimageElements();
        LogUtil.info(TAG,"The size is " + imagePathElements.size());
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void requestPermission() {
        requestPermissionsFromUser(new String[]{"ohos.permission.READ_USER_STORAGE"},1123);
//        if (verifyCallingOrSelfPermission(SystemPermission.READ_USER_STORAGE) != IBundleManager.PERMISSION_GRANTED) {
//            requestPermissionsFromUser(new String[]{SystemPermission.READ_USER_STORAGE}, 0X01);
//        }
    }
}
